package com.shuai.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuai.common.constant.AuthenticationConstant;
import com.shuai.common.constant.ErrorCode;
import com.shuai.common.exception.BusinessException;
import com.shuai.common.utils.MyJWTUtil;
import com.shuai.user.client.RedisForItrClient;
import com.shuai.user.domain.dto.ItrRankVO;
import com.shuai.user.domain.entity.InterfaceInfo;
import com.shuai.user.domain.request.InterfaceInfoSaveRequest;
import com.shuai.user.domain.vo.InterfaceInfoRankVO;
import com.shuai.user.domain.vo.InterfaceInfoVO;
import com.shuai.user.mapper.InterfaceInfoMapper;
import com.shuai.user.service.InterfaceInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bxs
 * @description 针对表【interface_info(接口信息表)】的数据库操作Service实现
 * @createDate 2024-08-03 11:46:14
 */
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
        implements InterfaceInfoService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Autowired
    private RedisForItrClient redisForItrClient;

    @Override
    public InterfaceInfo saveItr(InterfaceInfoSaveRequest interfaceInfoSaveRequest, String authentication) {
        Object payLoadItem = MyJWTUtil.getPayLoadItem(authentication, AuthenticationConstant.PAYLOAD_USERID);
        if (payLoadItem == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户登录信息过期");
        }
        Long userId = Long.valueOf(payLoadItem.toString());
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        interfaceInfo.setUserId(userId);
        BeanUtils.copyProperties(interfaceInfoSaveRequest, interfaceInfo);
        this.save(interfaceInfo);
        return interfaceInfo;
    }

    @Override
    public List<InterfaceInfoVO> listAll(String authentication) {
        Object payLoadItem = MyJWTUtil.getPayLoadItem(authentication, AuthenticationConstant.PAYLOAD_USERID);
        if (payLoadItem == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户登录信息过期");
        }
        List<InterfaceInfo> interfaceInfos = this.list();
        return interfaceInfos.stream().map(interfaceInfo -> {
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
            return interfaceInfoVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<InterfaceInfoRankVO> getInvokeRank(Integer topN) {
        List<ItrRankVO> ranks = redisForItrClient.getRank(topN);
        if (ranks == null || ranks.isEmpty()) {
            return new ArrayList<>();
        }
        Map<Long, ItrRankVO> itrRankVOMap = ranks.stream().collect(Collectors.toMap(ItrRankVO::getInterfaceId, itrRankVO -> itrRankVO));
        List<Long> ids = ranks.stream().map(ItrRankVO::getInterfaceId).collect(Collectors.toList());
        List<InterfaceInfo> interfaceInfos = this.listByIds(ids);
        return interfaceInfos.stream().map(interfaceInfo -> {
            InterfaceInfoRankVO interfaceInfoRankVO = new InterfaceInfoRankVO();
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoRankVO);
            interfaceInfoRankVO.setInvokeCount(itrRankVOMap.get(interfaceInfo.getId()).getInvokeCount());
            interfaceInfoRankVO.setAvgRpsTime(itrRankVOMap.get(interfaceInfo.getId()).getAvgRpsTime());
            return interfaceInfoRankVO;
        }).collect(Collectors.toList());
    }
}




